using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace DataProvider
{
    public static class EncodeUtils
    {

        public static string EncodeHtml(string strInput)
        {
            if (strInput == null)
            {
                return "";
            }
            StringBuilder builder = new StringBuilder("", strInput.Length * 2);
            foreach (char ch in strInput)
            {
                if ((((ch > '`') && (ch < '{')) || ((ch > '@') && (ch < '['))) || (((ch == ' ') || ((ch > '/') && (ch < ':'))) || (((ch == '.') || (ch == ',')) || ((ch == '-') || (ch == '_')))))
                {
                    builder.Append(ch);
                }
                else
                {
                    builder.Append("&#" + ((int)ch).ToString() + ";");
                }
            }
            return builder.ToString();
        }

        public static string EncodeHtmlAttr(string strInput)
        {
            if (strInput == null)
            {
                return "";
            }
            StringBuilder builder = new StringBuilder("", strInput.Length * 2);
            foreach (char ch in strInput)
            {
                if ((((ch > '`') && (ch < '{')) || ((ch > '@') && (ch < '['))) || (((ch > '/') && (ch < ':')) || (((ch == '.') || (ch == ',')) || ((ch == '-') || (ch == '_')))))
                {
                    builder.Append(ch);
                }
                else
                {
                    builder.Append("&#" + ((int)ch).ToString() + ";");
                }
            }
            return builder.ToString();
        }

        public static string EncodeJs(string strInput)
        {
            if (strInput == null)
            {
                return "''";
            }
            StringBuilder builder = new StringBuilder("'", strInput.Length * 2);
            foreach (char ch in strInput)
            {
                if ((((ch > '`') && (ch < '{')) || ((ch > '@') && (ch < '['))) || (((ch == ' ') || ((ch > '/') && (ch < ':'))) || (((ch == '.') || (ch == ',')) || ((ch == '-') || (ch == '_')))))
                {
                    builder.Append(ch);
                }
                else if (ch > '\x007f')
                {
                    builder.Append(@"\u" + TwoByteHex(ch));
                }
                else
                {
                    builder.Append(@"\x" + SingleByteHex(ch));
                }
            }
            builder.Append("'");
            return builder.ToString();
        }

        public static string EncodeXml(string strInput)
        {
            return EncodeHtml(strInput);
        }

        public static string EncodeXmlAttr(string strInput)
        {
            return EncodeHtmlAttr(strInput);
        }

        public static String EncodeSql(String sql)
        {
            if (String.IsNullOrEmpty(sql))
                return String.Empty;

            return sql.Replace("'", "''");
        }

        private static string SingleByteHex(char c)
        {
            uint num = c;
            return num.ToString("x").PadLeft(2, '0');
        }

        private static string TwoByteHex(char c)
        {
            uint num = c;
            return num.ToString("x").PadLeft(4, '0');
        }

        public static String EncodeJson(String data)
        {
            if (String.IsNullOrEmpty(data))
                return "";
            StringBuilder builder = new StringBuilder();
            foreach (char ch in data)
            {
                switch (ch)
                {
                    case '"':
                        builder.Append("\\\"");
                        break;

                    case '\\':
                        builder.Append(@"\\");
                        break;

                    case '\b':
                        builder.Append(@"\b");
                        break;

                    case '\f':
                        builder.Append(@"\f");
                        break;

                    case '\n':
                        builder.Append(@"\n");
                        break;

                    case '\r':
                        builder.Append(@"\r");
                        break;

                    case '\t':
                        builder.Append(@"\t");
                        break;

                    default:
                        if (char.IsLetterOrDigit(ch))
                        {
                            builder.Append(ch);
                        }
                        else if (char.IsPunctuation(ch))
                        {
                            builder.Append(ch);
                        }
                        else if (char.IsSeparator(ch))
                        {
                            builder.Append(ch);
                        }
                        else if (char.IsWhiteSpace(ch))
                        {
                            builder.Append(ch);
                        }
                        else if (char.IsSymbol(ch))
                        {
                            builder.Append(ch);
                        }
                        else
                        {
                            int num = ch;
                            builder.Append(@"\u" + num.ToString("x4"));
                        }
                        break;
                }
            }
            return builder.ToString();

        }

        public static String EncodeJsParmeter(String para, String quto)
        {
            if (String.IsNullOrEmpty(para))
                return "";
            return para = para.Replace('\''.ToString(), "\\x27").Replace("\"", "&quot;"); ;
        }
 
    }
}
